﻿using NDF.Data.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using UA.Data.Model;

namespace UA.Data
{
    public partial class UserRoleAccessor
    {
        public Role[] GetRolesByUserID(int userID)
        {
            using (DbContext ctx = this.GetDbContext())
            {
                var roles = from user in ctx.Set<User>()
                            join userRole in ctx.Set<UserRole>() on user.Key equals userRole.UserKey
                            join role in ctx.Set<Role>() on userRole.RoleKey equals role.Key
                            where user.ID == userID
                            select role;
                return roles.ToArray();
            }
        }

        public Role[] GetRolesByUserKey(Guid userKey)
        {
            using (DbContext ctx = this.GetDbContext())
            {
                var roles = from userRole in ctx.Set<UserRole>()
                            join role in ctx.Set<Role>() on userRole.RoleKey equals role.Key
                            where userRole.UserKey == userKey
                            select role;
                return roles.ToArray();
            }
        }

        public Role[] GetRolesByUserCode(string userCode)
        {
            using (DbContext ctx = this.GetDbContext())
            {
                var roles = from user in ctx.Set<User>()
                            join userRole in ctx.Set<UserRole>() on user.Key equals userRole.UserKey
                            join role in ctx.Set<Role>() on userRole.RoleKey equals role.Key
                            where user.Code == userCode
                            select role;
                return roles.ToArray();
            }
        }
    }
}
